package com.zt.dev.semiPhysicalEquipment.controller;

import com.zt.dev.common.ServerResponse;
import com.zt.dev.semiPhysicalEquipment.pojo.*;
import com.zt.dev.semiPhysicalEquipment.service.SemiPhysicalEquipmentService;
import com.zt.dev.semiPhysicalEquipment.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import static com.zt.dev.semiPhysicalEquipment.util.StrUtils.strToArry;

/**
 * @Author zhb
 * @Date 2020-2-10 09点45分
 * 半实物装备信息控制层
 */
@RestController
@RequestMapping("/hard")
public class SemiPhysicalEquipmentController {

    @Autowired
    private SemiPhysicalEquipmentService semiPhysicalEquipmentService;

    /**
     * 获取所有装备
     * @return
     */
    @RequestMapping("/findAllEquipment")
    public ServerResponse findAllEquipment() {
        List<SemiPhysicalEquipment> equipments= semiPhysicalEquipmentService.findAllEquipment();
        if (!CollectionUtils.isEmpty(equipments)){
            return ServerResponse.createBySuccess(equipments);
        }else {
            return ServerResponse.createByErrorMessage("数据为空");
        }
    }

    /**
     * 获取所有性能
     * @return
     */
    @RequestMapping("/findAllPerformances")
    public ServerResponse findAllPerformances(Integer equipmentId) {
        List<SemiPhysicalPerformance> performances= semiPhysicalEquipmentService.findAllPerformances(equipmentId);
        if (!CollectionUtils.isEmpty(performances)){
            return ServerResponse.createBySuccess(performances);
        }else {
            return ServerResponse.createByErrorMessage("数据为空");
        }
    }

    /**
     * 注册半实物装备信息
     * @param semiPhysicalEquipmentInfo
     * @return
     */
    @RequestMapping("/hardWareRegist")
    public ServerResponse hardWareRegist(@RequestBody SemiPhysicalEquipmentInfo semiPhysicalEquipmentInfo) {
        if (!StringUtils.isEmpty(semiPhysicalEquipmentInfo)) {
            Boolean b = semiPhysicalEquipmentService.hardWareRegist(semiPhysicalEquipmentInfo);
            if (b){
                return ServerResponse.createBySuccess("注册成功");
            }
            return ServerResponse.createByErrorMessage("注册失败");
        }
        return ServerResponse.createByErrorMessage("参数错误");
    }

    /**
     * 复制半实物装备信息
     * @return
     */
    @RequestMapping("/copyHardWare")
    public ServerResponse copyHardWare(String ids) {
        Boolean b = semiPhysicalEquipmentService.copyHardWare(ids);
        if (b){
            return ServerResponse.createBySuccess("复制成功");
        }
        return ServerResponse.createByErrorMessage("复制失败");
    }

    /**
     * 分页查询半实物装备信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllHardWare")
    public ServerResponse findAllHardWare(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage, int pageSize, Integer equipmentId) {
        PageBean<SemiPhysicalEquipmentInfo> pageBean = semiPhysicalEquipmentService.findAllHardWare(currentPage, pageSize,equipmentId);
        return ServerResponse.createBySuccess(pageBean);
    }

    /**
     *通过id查询半实物装备信息
     * @return
     */
    @RequestMapping("/findHardWareById")
    public ServerResponse findHardWareById(Integer id) {
        SemiPhysicalEquipmentInfo info = semiPhysicalEquipmentService.findHardWareById(id);
        if (!StringUtils.isEmpty(info)){
            return ServerResponse.createBySuccess(info);
        }
        return ServerResponse.createByErrorMessage("数据为空");
    }

    /**
     * 半实物装备信息编辑
     * @param semiPhysicalEquipmentInfo
     * @return
     */
    @RequestMapping("/editHardWareById")
    public ServerResponse editHardWareById(@RequestBody SemiPhysicalEquipmentInfo semiPhysicalEquipmentInfo) {
        int i = semiPhysicalEquipmentService.editHardWareById(semiPhysicalEquipmentInfo);
        if (i > 0) {
            return ServerResponse.createBySuccess("修改成功");
        }
        return ServerResponse.createByErrorMessage("修改失败");
    }

    /**
     * 单独修改使用信息
     * @param useInfo
     * @return
     */
    @RequestMapping("/editUseInfoById")
    public ServerResponse editUseInfoById(@RequestBody SemiPhysicalUseInfo useInfo) {
        int i = semiPhysicalEquipmentService.editUseInfoById(useInfo);
        if (i > 0) {
            return ServerResponse.createBySuccess("修改成功");
        }
        return ServerResponse.createByErrorMessage("修改失败");
    }

    /**
     * 半实物装备信息删除
     * @param ids
     * @return
     */
    @RequestMapping("/removeHardWareById")
    public ServerResponse removeHardWareById(String ids) {
        int[] a = strToArry(ids);
        Boolean b = semiPhysicalEquipmentService.removeHardWareById(a);
        if (b) {
            return ServerResponse.createBySuccess("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    public void export(
            @RequestParam(value = "startTime",required = false) String startTime,
            @RequestParam(value = "endTime",required = false)String endTime) {
        semiPhysicalEquipmentService.export(startTime,endTime);
    }

    /**
     * 导入
     * @param file
     * @throws Exception
     */
    @RequestMapping("/import")
    public ServerResponse importExcel(MultipartFile file){
        Boolean b = semiPhysicalEquipmentService.importExcel(file);
        if (b) {
            return ServerResponse.createBySuccess("导入成功");
        }
        return ServerResponse.createByErrorMessage("导入失败");
    }
}
